package org.example.service.Impl;

import lombok.extern.slf4j.Slf4j;
import org.example.dao.BookingRecordDao;
import org.example.dao.UserDao;
import org.example.domain.BookingRecord;
import org.example.domain.User;
import org.example.service.BookingRecordService;
import org.example.utils.ResponseUtil.ResponseResult;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

import static org.example.utils.ResponseUtil.StatusCode.*;

@Service
@Slf4j
public class BookingRecordServiceImpl implements BookingRecordService {

    //疫苗预约记录
    @Resource
    private BookingRecordDao bookingRecordDao;

    //账号列表
    @Resource
    private UserDao userDao;


    /**
     * 增加 疫苗预约记录
     */
    @Override
    public ResponseResult insertBookingRecord(BookingRecord record) {
        Integer userId = record.getUser_uid();
        //首先去数据库查找，是否存在该账号id
        User u = userDao.selectUserById(userId);
        //如果不存在该账号
        if (u == null){
            return new ResponseResult(DATA_NOT_EXIST,
                    "系统中无 id="+userId+" 的账号！");
        }
        else {
            //执行插入操作
            int n =bookingRecordDao.insertBookingRecord(record);
            if (n>0){
                return new ResponseResult(SERVICE_RUN_SUCCESS,record,
                        record.getUser_uid()+"号客户于"+record.getBooking_time() +" 预约疫苗成功！");
            }
            else {
                return new ResponseResult(DATA_NOT_EXIST,
                        record.getUser_uid()+"号客户预约疫苗失败！");

            }
        }
    }


    /**
     * 修改 记录：根据疫苗预约记录的id
     */
    @Override
    public ResponseResult updateBookingRecordById(BookingRecord record) {
        Integer id = record.getBr_id();
        if (id ==null || id == 0){
            return new ResponseResult(DATA_NOT_EXIST, "更新失败，预约记录的id值为空！");
        }
        //根据预约记录的id值，去数据库查找原来的数据
        BookingRecord r = bookingRecordDao.selectBookingRecordById(id);
        //如果前端传来对象的某个属性值为空，就把数据库中的原值存进去
        if (record.getUser_uid() == null || record.getUser_uid() == 0){
            record.setUser_uid(r.getUser_uid());
        }
        if (record.getBooking_time() == null || "".equals(record.getBooking_time() )){
            record.setBooking_time(r.getBooking_time());
        }
        if (record.getInoculated() == null || record.getInoculated()==0){
            record.setInoculated(r.getInoculated());
        }
        //执行真正的更新操作
        int n = bookingRecordDao.updateBookingRecordById(record);
        if (n>0){
            return new ResponseResult(SERVICE_RUN_SUCCESS,record,
                    "id="+id+"的疫苗预约记录，更新成功！");
        }
        else {
            return new ResponseResult(DATA_NOT_EXIST,
                    "id="+id+"的疫苗预约记录，更新失败！");

        }

    }


    /**
     * 物理删除：根据疫苗预约记录表的br_id
     */
    @Override
    public ResponseResult deleteBookingRecordById(Integer id) {
        int n = bookingRecordDao.deleteBookingRecordById(id);
        if (n>0){
            return new ResponseResult(SERVICE_RUN_SUCCESS,n,
                    "id="+id+"的疫苗预约记录，删除成功！");
        }
        else {
            return new ResponseResult(DATA_NOT_EXIST,
                    "id="+id+"的疫苗预约记录，删除失败！");

        }
    }


    /**
     * 根据预约记录表的br_id，查找唯一的对象
     */
    @Override
    public ResponseResult selectBookingRecordById(Integer id) {
        BookingRecord record = bookingRecordDao.selectBookingRecordById(id);
        if (record == null){
            return new ResponseResult(DATA_NOT_EXIST,"id="+id+" 的疫苗预约记录不存在");
        }
        else {
            List list = new ArrayList();
            list.add(record);
            return new ResponseResult(SERVICE_RUN_SUCCESS,list,"id="+id+" 的疫苗预约记录存在");
        }
    }


    /**
     * 根据用户的user_uid，查找多个预约记录
     */
    @Override
    public ResponseResult selectBookingRecordByUserId(Integer userId) {
        List<BookingRecord> records = bookingRecordDao.selectBookingRecordByUserId(userId);
        if (records == null || records.size() == 0){
            return new ResponseResult(DATA_NOT_EXIST,"无用户id="+userId+" 的疫苗预约记录");
        }
        else {
            return new ResponseResult(SERVICE_RUN_SUCCESS,records,"存在用户id="+userId+" 的疫苗预约记录");
        }
    }


    /**
     * 根据 预约时间booking_time，进行查找
     */
    @Override
    public ResponseResult selectBookingRecordLikeBooking_time(String bookingDate) {
        List<BookingRecord> records = bookingRecordDao.selectBookingRecordLikeBooking_time(bookingDate);
        if (records == null || records.size() == 0){
            return new ResponseResult(DATA_NOT_EXIST,bookingDate+" 无疫苗预约记录");
        }
        else {
            return new ResponseResult(SERVICE_RUN_SUCCESS,records,bookingDate+" 存在疫苗预约记录");
        }
    }


    /**
     * 根据用户的账号userAccount，进行模糊查询
     */
    @Override
    public ResponseResult selectBookingRecordLikeUserAccount(String userAccount) {
        // 根据用户账号userAccount，模糊查询到多个user对象
        List<User> users = userDao.selectUserLikeAccount(userAccount);
        if (users == null || users.size() == 0){
            return new ResponseResult(DATA_NOT_EXIST,"无账号与'"+userAccount+"' 相关的疫苗预约记录！");
        }

        List<BookingRecord> records = new ArrayList<>();
        int userId;
        //挨个遍历，获得user对象的userId，并根据userId获取到接种记录
        for (User user:users){
            userId = user.getUid();
            records.addAll(bookingRecordDao.selectBookingRecordByUserId(userId));
        }
        if (records == null || records.size() == 0){
            return new ResponseResult(DATA_NOT_EXIST,"无账号与'"+userAccount+"' 相关的疫苗预约记录！");
        }
        else {
            return new ResponseResult(SERVICE_RUN_SUCCESS,records,"存在账号与'"+userAccount+"' 相关的疫苗预约记录！");
        }
    }


    /**
     * 根据用户的用户名username，进行模糊查询
     */
    @Override
    public ResponseResult selectBookingRecordLikeUsername(String username) {
        // 根据用户账号userAccount，模糊查询到多个user对象
        List<User> users = userDao.selectUserLikeUsername(username);
        if (users == null || users.size() == 0){
            return new ResponseResult(DATA_NOT_EXIST,"无用户名与'"+username+"' 相关的疫苗预约记录！");
        }

        List<BookingRecord> records = new ArrayList<>();
        int userId;
        //挨个遍历，获得user对象的userId，并根据userId获取到接种记录
        for (User user:users){
            userId = user.getUid();
            records.addAll(bookingRecordDao.selectBookingRecordByUserId(userId));
        }
        if (records == null || records.size() == 0){
            return new ResponseResult(DATA_NOT_EXIST,"无用户名与'"+username+"' 相关的疫苗预约记录！");
        }
        else {
            return new ResponseResult(SERVICE_RUN_SUCCESS,records,"存在用户名与'"+username+"' 相关的疫苗预约记录！");
        }
    }



    /**
     * 根据预约时间升序查询 所有的预约记录
     */
    @Override
    public ResponseResult AscSelectAllByTime() {
        List records = bookingRecordDao.AscSelectAllByTime();
        if (records == null || records.size() ==0 ){
            return new ResponseResult(DATA_NOT_EXIST,null,"疫苗预约列表为空！");
        }
        return new ResponseResult(SERVICE_RUN_SUCCESS, records,"疫苗预约列表查询成功");
    }


    /**
     * 根据预约时间降序查询 所有的预约记录
     */
    @Override
    public ResponseResult DescSelectAllByTime() {
        List records = bookingRecordDao.DescSelectAllByTime();
        if (records == null || records.size() ==0 ){
            return new ResponseResult(DATA_NOT_EXIST,null,"疫苗预约列表为空！");
        }
        return new ResponseResult(SERVICE_RUN_SUCCESS, records,"疫苗预约列表查询成功");
    }

    /**
     * 查询所有疫苗预约记录
     */
    @Override
    public ResponseResult selectAllBookingRecord() {
        List records = bookingRecordDao.selectAllBookingRecord();
        if (records == null || records.size() ==0 ){
            return new ResponseResult(DATA_NOT_EXIST,null,"疫苗预约列表为空！");
        }
        return new ResponseResult(SERVICE_RUN_SUCCESS, records,"疫苗预约列表查询成功");
    }

}
